//导入express
const express = require("express");
const vertoken = require("../util/token");
const { exec } = require("../db/index");

//实例化router对象
const router = express.Router();

router.post("/", async (req, res) => {
  const { username, password } = req.body;
  console.log(req.body ,username)
  let sql;
  sql = `select * from user where username = '${username}'`;
  const isUser = await exec(sql);
  if (isUser.length == 0) {
    res.send({
      status: 0,
      msg: "没有这个用户",
    });
    return;
  }
  if (isUser[0].password !== password) {
    res.send({
      status: 0,
      msg: "密码错误",
    });
  } else {
    const { username, usertype } = isUser[0];
    const token = await vertoken.setToken(username, password);
    console.log(isUser);
    res.send({
      status: 1,
      msg: "登陆成功",
      token,
      userinfo: { username, usertype },
    });
  }
});

module.exports = router;
